class ArtistsController < ApplicationController
  filter_access_to :all
  
  # GET /artist/id
  # GET /artist/id.xml
  def show
    @artist = Artist.find(params[:id])

    if current_user
      @favorite = current_user.favorites.find(:first, 
                                              :conditions => ['favoritable_id = ? AND favoritable_type = ?', 
                                              @artist.id, 'Artist'])
    end

    #Used to determine if in edit mode, move to
    # session once we add them
    session[:edit_mode] = false
    store_location

    respond_to do |format|
      format.html
      format.xml
    end
  end

  # GET /artists/new
  # GET /artists/new.xml
  def new
    @artist = Artist.new

    if params[:name]
      @artist.name = params[:name]
    end

    respond_to do |format|
      format.html
      format.xml { render :xml => @artist }
    end
  end
  
  # GET /artists/edit
  def edit
    @artist = Artist.find(params[:id])
    
    if current_user
      @favorite = current_user.favorites.find(:first,
                                              :conditions => ['favoritable_id = ? AND favoritable_type = ?',
                                                             @artist.id, 'Artist'])
    end


    #Used to determine if in edit mode, move to
    # session once we add them
    session[:edit_mode] = true
    store_location
  end

  # POST /artists/id
  def create
    @artist = Artist.new(params[:artist])

    respond_to do |format|
      if @artist.save
        format.html { redirect_to(@artist) }
        format.xml { render :xml => @artist, :status => :created, :location => @artist }
      else
        format.html { render :action => "new" }
        format.xml { render:xml => @artist.errors, :status => :unprocessable_entity }
      end
    end 
  end

  # PUT /artists/id
  def update
    @artist = Artist.find(params[:id])

    # Will expand this later, remove meberships and what not
    if params[:make_band]
      @artist.is_group = true
    end

    if params[:make_person]
      @artist.is_group = false
    end

    if params[:images] && params[:images][:image]
      @artist.images.build(params[:images])
    end

    respond_to do |format|
      if @artist.update_attributes(params[:artist])
        flash[:notice] = 'Information Updated.'
        format.html { render :action => "edit" }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @artist.errors, :status => :unprocessable_entity }
      end
    end
  end

  #
  #
  def destroy

  end

  #Retrieves a list of releases and renders to a partial
  def release_set
    artist = Artist.find(params[:id])
    @releaseList = artist.get_releases(params[:type])
    render(:partial => 'release', :locals => { :category => params[:type], :releases => @releaseList } )
  end

end
